<?php
/**
 * User: jewens
 */
class Recharge extends MY_Admin_Controller
{
    public function index()
    {
    	$page = intval($this->input->get_post('page'));
    	$page = $page ? $page : 1;
    	
    	$cid = intval($this->input->get_post('cid'));
    	$start_time = trim($this->input->get_post('start_time'));
    	$end_time = trim($this->input->get_post('end_time'));
    	$uid = trim($this->input->get_post('uid'));
    	
    	$where = array();
    	
    	if(isset($_GET['is_paid']) && $_GET['is_paid'] >= 0){
    		$where['is_paid'] = $_GET['is_paid'];
    	}
    	
    	if($cid){
    		$where['cid'] = $cid;
    	}
    	 
    	if($start_time){
    		$where['create_time>'] = $start_time;
    	}
    	 
    	if($end_time){
    		$where['create_time<'] = $end_time;
    	}
    	 
    	if($uid){
    		$where['uid'] = $uid;
    	}
    	
    	$this->load->model('finance/model_finance_payment_conf','conf');
    	$cateInfo = $this->conf->getAllLists();
    	
        $this->load->model('finance/model_finance_payment','finance_payment');
        
        
        if($this->input->get_post('submit') == 'export')
        {
        	$financeInfo = $this->finance_payment->getAllLists($where);
        	
        	$export_data[] = array('支付单号', '时光号', '支付渠道', '应付', '实付', 'ip', '状态', '第三方支付订单号', '付款时间', '创建时间');
        	
        	foreach($financeInfo as $v)
        	{
        		$export_data[] = array(
        				$v['pid'],
        				$v['uid'],
        				isset($cateInfo[$v['cid']]['name'])?$cateInfo[$v['cid']]['name']:'',
        				$v['amount'],
        				$v['real_amount'],
        				$v['ip'],
        				$v['is_paid']==1?'已支付':'未支付',
        				$v['out_order_id'],
        				$v['pay_time'],
        				$v['create_time'],
        				);
        	}
        	
        	$this->load->service('common/service_common_helper');
        	$this->service_common_helper->create_xls($export_data, $filename='recharge_record_'.date('Y-m-d').'.xls');
        }
        
        
        $total_rows = $this->finance_payment->count($where);
        
        //分页
        $this->load->library('pagination');
         
        $config['total_rows'] = $total_rows;
        $config['per_page'] = 20;
        $config['base_url'] = '/finance/Recharge/index';
         
        $this->pagination->initialize($config);
        $show_page = $this->pagination->create_links();
        
        //读取数据
        $offset = ($page-1) * $config['per_page'];
        $financeInfo = $this->finance_payment->lists($offset,  $config['per_page'], $where);

        $amount_total = $this->finance_payment->getOne($where, 'sum(amount) as amount_total, sum(real_amount) as real_amount_total');

        $data = array(
            'financeInfo' => $financeInfo,
            'cateInfo'    => $cateInfo,
        	'show_page'   => $show_page,
        	'total_rows'  => $config['total_rows'],
        	'amount_total'=> $amount_total,
        );

        $this->load->view('admin/finance/recharge/index',$data);
    }
}